package com.scalar.chiptrack.operations.dataentry.actions;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.dataentry.forms.FGIShipLotsForm;
import com.scalar.chiptrack.operations.dataentry.InTransitLots;
import com.scalar.chiptrack.operations.dataentry.dao.FGIShipLotsDAO;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by Sudhakar.
 * Date: Jun 14, 2005
 * Time: 12:11:29 PM
 */
public class FGIShipLotsAction extends ChipTrackAction
{

    private static final String ACTION_NAME = "/fgiShipLot.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
            throws Exception
    {
        FGIShipLotsForm fgiShipLotsForm = ( FGIShipLotsForm ) form;

        if ( fgiShipLotsForm == null )
        {
            fgiShipLotsForm = new FGIShipLotsForm();
        }

        HttpSession session = request.getSession();

        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if ( user == null )
        {
            if( session.getAttribute( "ExtUser" ) != null )
            {
                fgiShipLotsForm.setUserType( 0 );
                user 				= ( User )session.getAttribute ( "ExtUser" );
            }
            else
            {
                session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
                return ( mapping.findForward( "login" ) );
            }
        }



        String action = request.getParameter( "action" );
        if ( action == null )
        {
            action = "";
        }
        request.setAttribute( "action", action );
        HashMap errors = new HashMap();
        HashMap info = new HashMap();

        if ( user != null && user.isActionAllowed ( Functionality.FGI_RECEIPTS, User.USER_ACTION_MODIFY ) )
        {
            request.setAttribute ( "showMessage", "FALSE" );
        }
        else
        {
            request.setAttribute ( "showMessage", "TRUE" );
        }


        if ( action.equals( "" ) )
        {

            ArrayList fgShipLotsList = FGIShipLotsDAO.loadAllTransitLots( "TEST", user, fgiShipLotsForm.getUserType() );

            fgiShipLotsForm.setFgShipLotsList( fgShipLotsList );
            request.setAttribute( "fgShipLotsList", fgShipLotsList );
            request.setAttribute( "CSLocations", FGIShipLotsDAO.loadClassStroeLocations( null, user, fgiShipLotsForm.getUserType() ) );

            return mapping.findForward( "success" );
        }
        if ( action.equals( "save" ) )
        {

            InTransitLots inTransitLots = null;
            ArrayList fgiShippedLotList = new ArrayList();
            ArrayList modifyShipQtyList = new ArrayList();

            fgiShippedLotList = fgiShipLotsForm.getFgShipLotsList();
            if ( fgiShippedLotList.size() > 0 )
            {
                for ( int i = 0; i < fgiShippedLotList.size(); i++ )
                {
                    inTransitLots = ( InTransitLots ) fgiShippedLotList.get( i );
                    if ( inTransitLots.isChecked() == true )
                    {
                        if( inTransitLots.getRecivedShipQty() == null )
                        {
                            errors.put( " Received Ship Qty should not be null ",null );
                            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
                            return mapping.findForward( "success" );
                        }

                        modifyShipQtyList.add( inTransitLots );
                    }
                }
            }
            try
            {   // To update the modified Ship Qty
                FGIShipLotsDAO.modifyShipQty( null, modifyShipQtyList.iterator() );
                //To send alert when quantity pulled from FGI Reciepts
                if( modifyShipQtyList.size() > 0 )
                {
                    FGIShipLotsDAO.notifyUserBlankFgiLots( modifyShipQtyList );
                }
                info.put( "Selected Lots are ShipQty updated Successfully ", null );
                request.setAttribute( SessionKeys.INFO_MESSAGES, info );

            }
            catch ( ScalarException se )
            {
                se.getRootCause().printStackTrace();
                errors.put( "Internal Server Error:", "Could not update the ShipQty " );
                request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            }

            ArrayList fgShipLotsList = FGIShipLotsDAO.loadAllTransitLots( "TEST", user, fgiShipLotsForm.getUserType() );
            fgiShipLotsForm.setFgShipLotsList( fgShipLotsList );
            request.setAttribute( "fgShipLotsList", fgShipLotsList );
            request.setAttribute( "CSLocations", FGIShipLotsDAO.loadClassStroeLocations( null, user, fgiShipLotsForm.getUserType() ) );

            return mapping.findForward( "success" );
        }

        return mapping.findForward( "success" );
    }
}
